package com.lmt.base;

import com.lmt.util.SystemConstant;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Date 2018/07/17
 *
 * @author MT-Lin
 */
@Component
public class BaseService {

    @Resource
    protected EntityManager entityManager;

    /**
     * 分页查询
     * @param sql sql语句
     * @param baseSearch 查询条件
     * @param lists 查询参数
     * @return 查询结果
     */
    public Map<String, Object> paginationSearch(StringBuffer sql, BaseSearch baseSearch, List... lists) {
        Query getResultQuery = entityManager.createNativeQuery(sql.toString());
        @SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
        String countBuffer = "SELECT COUNT(*) FROM (" + sql.toString() + ") AS COUNT_TABLE";
        // 设置参数
        int position = 1;
        Query countQuery = entityManager.createNativeQuery(countBuffer);
        for (List parameters : lists) {
            for (Object parameter : parameters) {
                countQuery.setParameter(position, parameter);
                getResultQuery.setParameter(position, parameter);
                position++;
            }
        }
        // 数据总数量
        Object totalElements = countQuery.getResultList().get(0);
        getResultQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        int currentPage = baseSearch.getCurrentPage() - 1;
        int pageSize = baseSearch.getPageSize();
        getResultQuery.setFirstResult(currentPage * pageSize);
        getResultQuery.setMaxResults(pageSize);
        List resultList = getResultQuery.getResultList();
        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put(SystemConstant.TOTAL_ELEMENTS, totalElements);
        resultMap.put(SystemConstant.CONTENT, resultList);
        return resultMap;
    }

    /**
     * 查询一列的数据
     * @param sql sql语句
     * @param parameters 查询参数
     * @return 查询结果
     */
    protected List nativeSqlSearchOneColumn(StringBuffer sql, List<Object> parameters) {
        Query nativeQuery = this.entityManager.createNativeQuery(sql.toString());
        int position = 1;
        for (Object parameter : parameters) {
            nativeQuery.setParameter(position, parameter);
            position++;
        }
        return nativeQuery.getResultList();
    }

    /**
     * 指定查询数据的数量
     * @param sql 查询语句
     * @param parameters 查询参数
     * @param dataSize 数据大小
     * @return 查询结果
     */
    protected List nativeSqlSearch(StringBuffer sql, List<Object> parameters, int dataSize) {
        Query nativeQuery = this.entityManager.createNativeQuery(sql.toString());
        int position = 1;
        for (Object parameter : parameters) {
            nativeQuery.setParameter(position, parameter);
            position++;
        }
        if (dataSize > 0) {
            nativeQuery.setMaxResults(dataSize);
        }
        nativeQuery.unwrap(NativeQueryImpl.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        return nativeQuery.getResultList();
    }
}
